package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("删除图书：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int index = -1;
        int currentSize = bookList.getUsedSize();
        for(int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (name.equals(book.getName())) {
                index = i;
                break;
            }
        }
        if(index == -1) {
            System.out.println("没有你要删除的书！");
            return;
        }
        for(int i = index; i < currentSize - 1; i++) {
            bookList.setBooks(i, bookList.getBook(i + 1));
        }
        bookList.setUsedSize(currentSize - 1);
        System.out.println("删除成功！");
    }
}